package com.wyh.yuhcloud.controller;

import com.wyh.yuhcloud.core.Result;
import com.wyh.yuhcloud.dto.PageDto;
import com.wyh.yuhcloud.dto.QueryInfoDto;
import com.wyh.yuhcloud.entity.File;
import com.wyh.yuhcloud.entity.PageBean;
import com.wyh.yuhcloud.service.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wyh
 * @version 1.0
 * @date 2022/2/24 16:53
 */
@RestController
@RequestMapping("/file")
public class FileController {

    private final Logger log = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private FileService fileService;



    @PostMapping("/getFile")
    public Result queryFile(@RequestBody PageDto pageDto){
        PageBean<File> filePageBean = fileService.queryFile(pageDto);
        log.info("id: "+pageDto.getUserId()+" 的文件查询信息: "+filePageBean);
        return new Result(filePageBean);
    }

    @PostMapping("/findFile")
    public Result findFile(@RequestBody QueryInfoDto queryInfoDto){
        PageBean<File> filePageBean = fileService.queryMap(queryInfoDto);
        return new Result(filePageBean);
    }

    @PostMapping("/upFile")
    public Result uploadFile(@RequestPart("file")MultipartFile file, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        if (file != null && userId != null) {
            try {
                String url = fileService.uploadFile(file, userId);
                log.info("上传的文件地址:{}, id:{}",url,userId);
                return new Result(200, url);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        log.info("上传失败 id:{}",userId);
        return new Result(500, "error");
    }


    @DeleteMapping("/delFile/{fileId}")
    public Result delFile(@PathVariable("fileId")String fileId, HttpServletRequest request) {
        if (fileId != null) {
            try {
                String userId = request.getHeader("userId");
                String result = fileService.delFile(fileId,userId);
                return new Result(200, result);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new Result(500, "error");
    }

    @DeleteMapping("/delFile")
    public Result delFileList(@RequestBody List<String> delList, HttpServletRequest request) {
        if (!delList.isEmpty()) {
            String userId = request.getHeader("userId");
            try {
                for (String id : delList) {
                   fileService.delFile(id,userId);
                }
                return new Result(200, "success");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new Result(500, "error");
    }

    @PostMapping("/filesTotal/{userId}")
    public Result filesTotal(@PathVariable("userId")String userId){
        return  new Result(200,fileService.fileTotal(userId).toString());
    }


}
